package boot

import (
	"github.com/go-redis/redis/v8"
	"sync"
	"time"
	"user-server/src/util"
)

type RedisEngine struct {
	*redis.Client
}

var (
	redisEngine *RedisEngine
	redisOnce   sync.Once
)

func GetRedisEngine() *RedisEngine {
	return redisEngine
}

func NewRedisEngine() {
	appInfo := util.GetYamlConfig()

	rdb := redis.NewClient(&redis.Options{
		Addr:         appInfo.Redis.Host,
		Password:     "",
		DB:           0,
		DialTimeout:  10 * time.Second,
		ReadTimeout:  30 * time.Second,
		WriteTimeout: 30 * time.Second,
		PoolSize:     10,
		PoolTimeout:  30 * time.Second,
	})

	//https://geektutu.com/post/hpg-sync-once.html ,单例模式
	redisOnce.Do(func() {
		rdh := new(RedisEngine)
		rdh.Client = rdb
		redisEngine = rdh
	})
}
